// 消息类型
const MSG_NORMAL = 0;                           // background.js 不必改动发送的消息, 可以直接发送给content.js
const MSG_SPEFIFIC = 1;                         // background.js 需要重新包装消息

var ports = [];
chrome.runtime.onConnect.addListener(function(port) {
    if (port.name !== "devtools") return;
    ports.push(port);
    // Remove port when destroyed (eg when devtools instance is closed)
    port.onDisconnect.addListener(function() {
        var i = ports.indexOf(port);
        if (i !== -1) ports.splice(i, 1);
    });
    port.onMessage.addListener(function(msg) {
        // Received message from devtools. Do something:
        console.log('Received message from devtools page', msg);

        if (msg.todo == "test"){
            console.log("==test");
            chrome.tabs.sendMessage(msg.tabId, {todo: msg.todo, text: msg.text});
        }else if(msg.todo == "preview"){
            console.log("==preview");
            chrome.tabs.sendMessage(msg.tabId, {todo: msg.todo, xpath: msg.xpath});
        }else if(msg.todo == "getBasicInfo"){
            console.log("==get basic info");
            chrome.tabs.sendMessage(msg.tabId, {todo: msg.todo});
        }else if(msg.todo == "getSubRange"){
            console.log("==getSubRange==");
            chrome.tabs.sendMessage(msg.tabId, {todo: msg.todo});
        }else if(msg.todo == "autoSubRange"){
            console.log("==auto create sub range==");
            chrome.tabs.sendMessage(msg.tabId, {todo: msg.todo, xpath: msg.xpath, max_range_xpath: msg.max_range_xpath});
        }else if(msg.todo == "activePreview"){
            console.log("==active preview==");
            chrome.tabs.sendMessage(msg.tabId, {todo: msg.todo, bingding_data_set: msg.bingding_data_set, data_set_list: msg.data_set_list});
        }

        // new
        if (msg.msg_type === MSG_NORMAL){
            console.log("==normal type msg==");
            chrome.tabs.sendMessage(msg.tabId, msg);
        }
    });
});

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if(request.todo == "basicInfo"){
        let url_cookie;

        // 写在里面是因为这个回调函数再最后才执行
        chrome.cookies.getAll({url: request.basicInfo.url}, function(cookies) {
            console.log(cookies);
            url_cookie = cookies;
            let cookies_length;

            if(url_cookie){
                cookies_length = cookies.length;
            }else{
                cookies_length = 0;
            }
            
            let msg = {
                todo: "basicInfo",
                url: request.basicInfo.url,
                cookies: url_cookie,
                cookies_length: cookies_length
            }

            ports.forEach(function(port){
                port.postMessage(msg);
            });

        });
    }else if(request.todo == "subRangeXpath"){
        ports.forEach(function(port){
            port.postMessage({todo: "subRangeXpath", xpath: request.xpath, sub_range: request.sub_range});
        });
    }

    //new
    if (request.msg_type == MSG_NORMAL){
        ports.forEach(function(port){
            port.postMessage(request);
        });
    }
    sendResponse({farewell: "goodbye"});
});

// Function to send a message to all devtools.html views:
function notifyDevtools(msg) {
    ports.forEach(function(port) {
        port.postMessage(msg);
    });
}